package com.thlws.study.redismq.internal.queue;

import cn.hutool.core.util.RandomUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * static private final String MQ_NAME_KEY_1ST = "vc.fs.mq1";
 * static private final String MQ_NAME_KEY_2ND = "vc.fs.mq2";
 * static private final String MQ_NAME_KEY_3RD = "vc.fs.mq3";
 * static private final String MQ_NAME_KEY_1ST_BAK = "vc.fs.mq1.bak";
 * static private final String MQ_NAME_KEY_2ND_BAK = "vc.fs.mq2.bak";
 * static private final String MQ_NAME_KEY_3RD_BAK = "vc.fs.mq3.bak";
 *
 * @author HanleyTang 2019-08-07
 */
public class QueueSelector {

    private static QueueSelector instance = new QueueSelector();
    private static List<Queue> queues = new ArrayList<>(3);

    static {
        queues.add(new Queue("vc.fs.mq1","vc.fs.mq1.bak"));
        queues.add(new Queue("vc.fs.mq2","vc.fs.mq2.bak"));
        queues.add(new Queue("vc.fs.mq3","vc.fs.mq3.bak"));
    }

    private QueueSelector(){
    }

    public static QueueSelector getInstance() {
        return instance;
    }

    public Queue select(){
        int pos = RandomUtil.randomInt(0, 3);
        return queues.get(pos);
    }

    public String selectQueueName(){
        int pos = RandomUtil.randomInt(0, 3);
        return queues.get(pos).getQueueName();
    }

    public Queue select(int position){
        if (position >= queues.size()) {
            throw new IndexOutOfBoundsException("该位置不存在元素");
        }
        return queues.get(position);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            Queue queue = QueueSelector.getInstance().select();
            System.out.println(queue.toString());
        }
    }
}
